package com.kyxx.topics.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kyxx.topics.R;
import com.kyxx.topics.activity.JoinMemberActivity;
import com.kyxx.topics.activity.MemberDeatailActivity;
import com.kyxx.topics.activity.ViewPagerActivity;
import com.kyxx.topics.bean.MemberDetailBean;
import com.kyxx.topics.bean.MemberPromotion;
import com.kyxx.topics.bean.MembershipCardBean;
import com.kyxx.topics.net.UrlUtils;
import com.kyxx.topics.utils.ScreenSizeUtils;
import com.kyxx.topics.utils.StringUtil;
import com.kyxx.topics.view.CircleImageView;
import com.kyxx.topics.zxing.encode.EncodingHandler;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class MemberPointAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
    private List<MemberPromotion> promotions;
    private Context ctx;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;
    private String[] bitmaps;

    public MemberPointAdapter(Context paramContext, List<MemberPromotion> promotion){
        this.ctx = paramContext;
        this.promotions = promotion;
        this.bitmaps = new String[promotions.size()];
        options = new DisplayImageOptions.Builder()
                // 设置图片在下载期间显示的图片
                .showImageOnLoading(R.mipmap.image_default)
                // 设置图片Uri为空或是错误的时候显示的图片
                .showImageForEmptyUri(R.mipmap.image_default)
                // 设置图片加载/解码过程中错误时候显示的图片
                .showImageOnFail(R.mipmap.image_default)
                .cacheInMemory(true)
                // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)
                // 设置下载的图片是否缓存在SD卡中
                .bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型
                // .displayer(new RoundedBitmapDisplayer(9999))
                .resetViewBeforeLoading(false)
                .build();
    }

    @Override
    public int getItemCount() {
        return  getContentItemCount();
    }


    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup paramViewGroup, int paramInt){
        return new ContentViewHolder(LayoutInflater.from(this.ctx).inflate(R.layout.item_member_detail, paramViewGroup, false));
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        imageLoader.displayImage(UrlUtils.BASEHTTP + promotions.get(position).getImageurl(),((ContentViewHolder) holder).iv_activity_icon, options);
        bitmaps[position] = UrlUtils.BASEHTTP + promotions.get(position).getImageurl();
        ((ContentViewHolder) holder).iv_activity_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ctx.startActivity(new Intent(ctx,ViewPagerActivity.class).putExtra("bitmap",bitmaps).putExtra("index",(position)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
            }
        });
    }
    public int getContentItemCount() {
        return promotions == null?0:this.promotions.size();
    }
    public class ContentViewHolder extends RecyclerView.ViewHolder{
        ImageView iv_activity_icon;

        public ContentViewHolder(View itemView) {
            super(itemView);
            iv_activity_icon = (ImageView) itemView.findViewById(R.id.iv_activity_icon);
        }
    }

    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
        if(lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams) {
            StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
            p.setFullSpan(holder.getLayoutPosition() == 0);
        }
    }
    private void  dialog(String code){
        Dialog dialog = new Dialog(ctx);
        View mDialogView =  View.inflate(ctx, R.layout.dialog_scan_code, null);
        ImageView img = (ImageView) mDialogView.findViewById(R.id.iv_scan_code);
        TextView tv = (TextView) mDialogView.findViewById(R.id.tv_scan_code_num);
        mDialogView.setMinimumHeight((int) (ScreenSizeUtils.getInstance(ctx).getScreenHeight() * 0.2f));
        dialog.setContentView(mDialogView);
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (ScreenSizeUtils.getInstance(ctx).getScreenWidth() * 0.88f);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(lp);
        img.setImageBitmap(createBarCode(code));
        tv.setText(code);
        dialog.show();
    }
    private Bitmap createBarCode(String key) {
        Bitmap qrCode = null;
        try {
            qrCode = EncodingHandler.createBarCode(key, (int) (ScreenSizeUtils.getInstance(ctx).getScreenWidth() * 0.7f), (int) (ScreenSizeUtils.getInstance(ctx).getScreenHeight() * 0.17f));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return qrCode;
    }
}